#include "setweb.h"
#include "ui_setweb.h"
#include<qdebug.h>

Setweb::Setweb(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Setweb)
{
    ui->setupUi(this);

    this->setWindowTitle("设置网站");

}

Setweb::~Setweb()
{
    delete ui;
}

void Setweb::settext()
{
    QString str[3];

    //数据库:
     QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    if(mysql.open()==true)
    {
        for(int i=0;i<3;i++)
        {
            QSqlQuery query;
            query.exec(QString("select * from web where id = %1").arg(i));
            if(query.first()==true)
                str[i]=query.value("webname").toString();

        }

        mysql.close();
    }

    ui->lineEdit->setText(str[0]);
    ui->lineEdit_2->setText(str[1]);
    ui->lineEdit_3->setText(str[2]);
}

void Setweb::on_checkBox_stateChanged(int arg1)
{
    if(ui->checkBox_2->isChecked()==true||ui->checkBox_3->isChecked()==true)
    {
        ui->checkBox->setCheckable(false);
        return;
    }
    if(ui->checkBox->isChecked()==true)
    {
        qDebug()<<"test";
        QString str[3];
        str[0]=ui->lineEdit->text();
        str[1]=ui->lineEdit_2->text();
        str[2]=ui->lineEdit_3->text();

        //数据库:
         QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          mysql = QSqlDatabase::database("qt_sql_default_connection");
        else
          mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        if(mysql.open()==true)
        {
            QSqlQuery query;
            query.exec("DROP TABLE web");
            query.exec("create table web(id int primary key, webname varchar(500))");
            for(int i=0;i<3;i++)
            {
                query.exec(QString("insert into web(id,webname) values(%1,'%2')").arg(i).arg(str[i]));
            }

            mysql.close();

            emit signal_addover_web(0);
        }
    }
    return;
}

void Setweb::on_checkBox_2_stateChanged(int arg1)
{
    if(ui->checkBox->isChecked()==true||ui->checkBox_3->isChecked()==true)
    {
        ui->checkBox_2->setCheckable(false);
        return;
    }

    if(ui->checkBox->isChecked()==true)
    {
        qDebug()<<"test";
        QString str[3];
        str[0]=ui->lineEdit->text();
        str[1]=ui->lineEdit_2->text();
        str[2]=ui->lineEdit_3->text();
        //数据库:
         QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          mysql = QSqlDatabase::database("qt_sql_default_connection");
        else
          mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        if(mysql.open()==true)
        {
            QSqlQuery query;
            query.exec("DROP TABLE web");
            query.exec("create table web(id int primary key, webname varchar(500))");
            for(int i=0;i<3;i++)
            {
                query.exec(QString("insert into web(id,webname) values(%1,'%2')").arg(i).arg(str[i]));
            }

            mysql.close();

            emit signal_addover_web(1);
        }
    }
    return;
}

void Setweb::on_checkBox_3_stateChanged(int arg1)
{
    if(ui->checkBox_2->isChecked()==true||ui->checkBox->isChecked()==true)
    {
        ui->checkBox_3->setCheckable(false);
        return;
    }

    if(ui->checkBox->isChecked()==true)
    {
        qDebug()<<"test";
        QString str[3];
        str[0]=ui->lineEdit->text();
        str[1]=ui->lineEdit_2->text();
        str[2]=ui->lineEdit_3->text();
        //数据库:
         QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          mysql = QSqlDatabase::database("qt_sql_default_connection");
        else
          mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        if(mysql.open()==true)
        {
            QSqlQuery query;
            query.exec("DROP TABLE web");
            query.exec("create table web(id int primary key, webname varchar(500))");
            for(int i=0;i<3;i++)
            {
                query.exec(QString("insert into web(id,webname) values(%1,'%2')").arg(i).arg(str[i]));
            }

            mysql.close();

            emit signal_addover_web(2);
        }
    }
    return;
}
